package live.ifox.stage;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import live.ifox.base.BaseRestActivity;
import live.ifox.factory.Entry;
import live.ifox.factory.ID;
import live.ifox.factory.Url;
import live.ifox.helper.AdapterHelper;
import live.ifox.helper.CommonHelper;

public class FeedbackEditor extends BaseRestActivity {
    boolean updateFlag;
    Spinner spinner;
    List<String> categories;
    EditText et_category, et_detail;
    int _id;
    String _category, _detail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feedback_editor);

        updateFlag = false;
        entry = Entry.Feedback;
        baseUrl = Url.Feedback;
        spinner = findViewById(R.id.sp_category);
        et_category = findViewById(R.id.category);
        et_detail = findViewById(R.id.detail);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                et_category.setText(categories.get(position));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        _id = getIntent().getIntExtra("id", 0);
        if (_id > 0) {
            updateFlag = true;
            Button btn_submit = findViewById(R.id.submit);
            btn_submit.setText(R.string.update);
            _category = getIntent().getStringExtra("category");
            _detail = getIntent().getStringExtra("detail");
            et_detail.setText(_detail);
            et_detail.setFocusable(true);
        }
        updateCategory();
    }

    void updateCategory() {
        httpGet(baseUrl + "get_categories");
    }

    public void onClean_Click(View view) {
        if (!et_category.getText().toString().isEmpty())
            et_category.setText("");

        if (!et_detail.getText().toString().isEmpty())
            et_detail.setText("");
    }

    public void onSubmit_Click(View view) {
        String category = et_category.getText().toString();
        if (category.isEmpty()) {
            CommonHelper.showMessage(this, "类别不能为空！");
            return;
        }

        String detail = et_detail.getText().toString();
        if (detail.isEmpty()) {
            CommonHelper.showMessage(this, "内容不能为空！");
            return;
        }

        Map<String, Object> map = new HashMap<>();
        if (!updateFlag) {
            map.put("category", category);
            map.put("detail", detail);
            httpPost(Url.Feedback_INSERT, JSON.toJSONString(map));
        } else {
            if (!_category.equals(category))
                map.put("category", category);

            if (!_detail.equals(detail))
                map.put("detail", detail);

            if (map.isEmpty())
                return;

            map.put("id", _id);
            httpPost(Url.Feedback_UPDATE, JSON.toJSONString(map));
        }

        setResult(ID.RESULT_DONE);
        finish();
    }

    @Override
    public void processReply(JSONObject rootObject) {
        JSONArray array = AdapterHelper.verifyObject(rootObject,
                "entry", entry, "categories");
        if (array.isEmpty())
            return;

        categories = array.toJavaList(String.class);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_dropdown_item);
        adapter.addAll(categories);
        spinner.setAdapter(adapter);
        if (updateFlag) {
            if (categories.contains(_category))
                spinner.setSelection(categories.indexOf(_category));
        }
    }
}
